//
//  QSNumberFormatter.swift
//  TestSwift
//
//  Created by Song on 2018/7/23.
//  Copyright © 2018年 Song. All rights reserved.
//

import UIKit

class QSNumberFormatter: NSObject {
    /// 数字格式化
    ///
    /// - Parameters:
    ///   - numString: 数字字符串
    ///   - numStyle: 数字格式
    ///   - roundingIncrement: 保存到第几位小数，默认0.01
    ///   - maximumFractionDigits: 小数部分最大位数
    ///   - roundingMode: 四舍五入模式，不做四舍五入设置为  .floor
    ///   - isUsesGroupingSeparator: 是否使用分组符号，默认true
    ///   - groupingSize: 分组时每组的位数大小
    ///   - groupingSeparator: 分组的分隔符
    ///   - positivePrefix: 正数前缀
    ///   - negativePrefix: 负数前缀
    ///   - positiveSuffix: 正数后缀
    ///   - negativeSuffix: 负数后缀
    ///   - percentSymbol: 百分号符号
    ///   - currencySymbol: 货币符号
    /// - Returns: 格式化后的数字字符串
    class func qs_numberFormatter(numString: String,
                                  numStyle: NumberFormatter.Style = .decimal,
                                  roundingIncrement: NSNumber? = 0.01,
                                  maximumFractionDigits: Int = 3,
                                  roundingMode: NumberFormatter.RoundingMode = .halfEven,
                                  isUsesGroupingSeparator: Bool = true,
                                  groupingSize: Int = 3,
                                  groupingSeparator: String? = nil,
                                  positivePrefix: String? = nil,
                                  negativePrefix: String? = nil,
                                  positiveSuffix: String? = nil,
                                  negativeSuffix: String? = nil,
                                  percentSymbol: String? = nil,
                                  currencySymbol: String? = nil) -> String? {
        
        let formatter = NumberFormatter()
        formatter.numberStyle = numStyle
        formatter.usesGroupingSeparator = isUsesGroupingSeparator
        formatter.maximumFractionDigits = maximumFractionDigits
        
        if roundingIncrement != nil {
            formatter.roundingIncrement = roundingIncrement
        }
        
        formatter.roundingMode = roundingMode
        
        // 把字符串转换为NSNumber类型
        let num = formatter.number(from: numString)
        if num == nil {
            return nil
        }
        
        if groupingSize >= 0 {
            formatter.groupingSize = groupingSize
        }
        
        if isUsesGroupingSeparator && groupingSize >= 0 && groupingSeparator != nil {
            formatter.groupingSeparator = groupingSeparator
        }
        
        if positivePrefix != nil {
            formatter.positivePrefix = positivePrefix
        }
        
        if negativePrefix != nil {
            formatter.negativePrefix = negativePrefix
        }
        
        if positiveSuffix != nil {
            formatter.positiveSuffix = positiveSuffix
        }
        
        if negativeSuffix != nil {
            formatter.negativeSuffix = negativeSuffix
        }
        
        if numStyle == .percent && percentSymbol != nil {
            formatter.percentSymbol = percentSymbol
        }
        
        if numStyle == .currency && currencySymbol != nil {
            formatter.currencySymbol = currencySymbol
        }
        
        // 格式化数字
        return formatter.string(from: num!)
    }
    
    /// 乘法
    ///
    /// - Parameters:
    ///   - numString: 数字字符串
    ///   - numStyle: 数字格式，默认小数
    ///   - multiplier: 乘数
    ///   - maximumFractionDigits: 小数部分最大位数，默认3位
    /// - Returns: 做乘法后的数字字符串
    class func qs_multiplier(numString: String,
                             numStyle: NumberFormatter.Style = .decimal,
                             multiplier: NSNumber = 1.0,
                             maximumFractionDigits: Int = 3,
                             isUsesGroupingSeparator: Bool = true) -> String? {
        let formatter = NumberFormatter()
        formatter.numberStyle = numStyle
        formatter.maximumFractionDigits = maximumFractionDigits
        formatter.usesGroupingSeparator = isUsesGroupingSeparator
        
        // 把字符串转换为NSNumber类型
        let num = formatter.number(from: numString)
        if num == nil {
            return nil
        }
        
        formatter.multiplier = multiplier
        
        // 格式化数字
        return formatter.string(from: num!)
    }
}
